<?php
/**
 * Created by PhpStorm
 * User: Kong
 * Date: 2025/9/25
 * Time: 14:38
 */

namespace App\Service\Admin;

use App\Constants\ErrorCode;

class MenuService
{
    /**
     * 删除菜单.
     * @throws \Exception
     * @return null|bool|int|mixed
     */
    public static function destroyMenu(int $id)
    {
        $menu = \App\Model\Menu::query()->find($id);
        if (! $menu) {
            throw new \Exception(ErrorCode::getMessage(ErrorCode::DATA_NOT_EXIST), ErrorCode::DATA_NOT_EXIST);
        }
        // 查找次菜单下面是否还有其他子的菜单
        $childMenu = \App\Model\Menu::query()->where('pid', $menu->id)->get();
        if ($childMenu->count() > 0) {
            throw new \Exception('此菜单下面还有未删除的子菜单，请先删除子菜单');
        }
        $rt = $menu->delete();
        if (! $rt) {
            throw new \Exception(ErrorCode::getMessage(ErrorCode::DELETE_ERROR), ErrorCode::DELETE_ERROR);
        }
        return [];
    }
}